package com.stone.boot.stoneboot.module.serverinfo.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.stone.boot.stoneboot.module.serverinfo.controller.vo.ServerInfoUploadVO;
import com.stone.boot.stoneboot.module.serverinfo.entity.ServerInfoDO;
import com.stone.boot.stoneboot.module.serverinfo.mapper.ServerInfoMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ServerInfoServiceImpl implements ServerInfoService {

    @Resource
    private ServerInfoMapper serverInfoMapper;

    @Override
    public void register(ServerInfoUploadVO serverInfoUploadVO) {
        ServerInfoDO serverInfoDO = BeanUtil.copyProperties(serverInfoUploadVO, ServerInfoDO.class);

        ServerInfoDO serverInfoDb = serverInfoMapper.selectOne(new LambdaQueryWrapper<ServerInfoDO>()
                .eq(ServerInfoDO::getIp, serverInfoDO.getIp())
                .eq(ServerInfoDO::getPort, serverInfoDO.getPort())
                .eq(ServerInfoDO::getName, serverInfoDO.getName())
        );

        if (serverInfoDb == null) {
            // 用 Date 获取当前时间
            serverInfoDO.setCreateTime(new Date());
            serverInfoMapper.insert(serverInfoDO);
        } else {
            serverInfoDb.setUpdateTime(new Date());
            serverInfoMapper.updateById(serverInfoDb);
        }

    }
}
